function DefenseTower(options) {
	this.range = options.range;
	this.reloadTime = options.reloadTime;  // cicles to recover 10% of life
	this.price = options.price;
	this.damage = options.damage;
	
	if(options.position) {
		this.setPosition(options.position);
	} else if(options.lat && options.lng) {
		this.setPosition(new google.maps.Latlng(options.lat, options.lng));
	} else {
		alert("Position is not well-defined.")
	}
	
	this.setMap(options.map);
	this.setIcon(options.icon);
	
	if(options.title) {
		this.setTitle(options.title);
	}

	this.charge = 0; // % of charge to be ready
	label = new Label({map: options.map}, 100, "charge");
    label.bindTo('life', this, 'charge');
    label.bindTo('position', this, 'position');
    this.label = label;

    this.raiseChargeIn = function(charge) {
    	if (this.charge == 100) return;
    	
    	newCharge = this.charge + charge;
    	if (newCharge >= 100) this.charge = 100;
    	else this.charge = newCharge;
        this.label.draw();
    };

    this.discharge = function() {
    	this.charge = 0;
    }
};

DefenseTower.prototype = new google.maps.Marker();

DefenseTower.Type = {
	FAST: 1,
	MEDIUM: 2,
	SLOW: 3
};

function DefenseTowerFactory() {};

DefenseTowerFactory.create = function(options) {
	switch(options.type) {
		case DefenseTower.Type.FAST:
			return new DefenseTower({
					position: options.position, 
					map: options.map,
					reloadTime: 1, 
					price: priceOfType(1), 
					range: rangeOfType(1), 
					damage: 20,
					icon:"./img/tower1.png"
				});
		case DefenseTower.Type.MEDIUM:
			return new DefenseTower({
					position: options.position, 
					map: options.map,
					reloadTime: 2, 
					price: priceOfType(2), 
					range: rangeOfType(2), 
					damage: 30,
					icon:"./img/tower2.png"
				});
		case DefenseTower.Type.SLOW:
			return new DefenseTower({
					position: options.position, 
					map: options.map,
					reloadTime: 3, 
					price: priceOfType(3), 
					range: rangeOfType(3), 
					damage: 40,
					icon:"./img/tower3.png" 
				});
	}
};

function rangeOfType(type) {
	// to independ of zoom, it returns a percentage of the biggest path (which is the range)
	switch(type) {
		case 1:
			return 6;
		case 2:
			return 9;
		case 3:
			return 3;
	}
}

function priceOfType(type) {
	switch(type) {
		case 1:
			return 50;
		case 2:
			return 80;
		case 3:
			return 100;
	}
}
